package com.devicemantra.coordinate.ds.tests;
import com.devicemantra.coordinate.dataobjects.*;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.devicemantra.coordinate.dataobjects.CDepartmentFactory;
import com.devicemantra.coordinate.exceptions.operations.CDepartmentDoesNotExistsException;
import com.devicemantra.coordinate.util.*;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;

@SuppressWarnings("serial")
public class CopyOfGetFrames extends HttpServlet 
{
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException 
	{
		resp.setContentType("text/html");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String lDeptName = (String) req.getParameter("dept_name");
		String lEmirateName = (String) req.getParameter("emirate");
		
		if( (null == lDeptName ) && (null == lEmirateName))
		{
			System.out.println("Error : Either Emirate or Department must be selected");
			return;
		}
		
		
		
		pm = PMF.get().getPersistenceManager();
		javax.jdo.Query q = pm.newQuery("select mLocationKey from " + CPoint.class.getName());
	    List ids = (List) q.execute();
	    //resp.getOutputStream().print("Size of list is " + ids.size());
	    Iterator<Key> lIterKeys = ids.iterator();
	    
	    while( lIterKeys.hasNext())
	    {
	    	Key lKey = (Key) lIterKeys.next();
	    	Key lParentKey = lKey.getParent();
	    	//resp.getOutputStream().print("<BR> Child is " + KeyFactory.keyToString(lKey));
	    	//resp.getOutputStream().print(" for parent - " + KeyFactory.keyToString(lParentKey) + "<BR>");
	    	
	    	CPoint lLoc = pm.getObjectById(CPoint.class,lKey);
	    	CLocation lFrame = pm.getObjectById(CLocation.class,lParentKey);
	    	CLocation lNewFrame = pm.detachCopy(lFrame);
	    	resp.getOutputStream().println("**********************");
	    	//resp.getOutputStream().println("<BR>Frame in [ " + lFrame.getEmirate().toUpperCase() + " ]<BR>  (" + lLoc.getLatitude() + "," + lLoc.getLongitude() + ") "+  "<BR> Owned by "   + lFrame.getOwningDepartment()  + "<BR> Description :" + lFrame.getFrameDescription() + "<BR> Address : " + lFrame.getAddress() + "<BR><BR>");
	    	try {
				System.out.println(getInfoAsXml(lNewFrame));
			} catch (CDepartmentDoesNotExistsException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	resp.getOutputStream().println("**********************");
	    }
	    pm.close();
		//resp.getOutputStream().println("Success" );
	}
	
	public String getInfoAsXml(CLocation pFrame) throws CDepartmentDoesNotExistsException
	{
		
		CXMLBuilder lXmlBuilder = new CXMLBuilder();
		
		/* First task is to get the url for the department logo */
		CDepartment lDept = CDepartmentFactory.getDepartment(pFrame.getOwningDepartment().getDepartmentName());
		
		/* Start building the xml */
		lXmlBuilder.AddOpeningTag("xml");
		lXmlBuilder.AddOpeningTag("CLocation");
		lXmlBuilder.AddNode("CFrameId", pFrame.getLocationId());
		lXmlBuilder.AddNode("CLat", pFrame.getLatitude());
		lXmlBuilder.AddNode("CLong", pFrame.getLatitude());
		lXmlBuilder.AddNode("CEmirate", pFrame.getEmirate());
		lXmlBuilder.AddNode("CAddress", pFrame.getAddress());
		lXmlBuilder.AddNode("CDepartment", pFrame.getOwningDepartment().getDepartmentName());
		lXmlBuilder.AddNode("CDepartmentLogo", lDept.getLogo().getUrl());
		lXmlBuilder.AddNode("CDescription", pFrame.getLocationDescription());
		lXmlBuilder.AddClosingTag("CLocation");
		lXmlBuilder.AddClosingTag("xml");
		return lXmlBuilder.getXml();
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws IOException 
	{
		doPost(req,resp);
	}
}
